package com.example.leetdemo.process;

import java.util.Stack;

public class Solution2434 {

    public String robotWithString(String s) {
        StringBuffer p = new StringBuffer();

        Stack<Character> t = new Stack<>();

        for(int i = 0; i < s.length(); i++) {
            char w = s.charAt(i);
            while(t.size() > 0 && w > t.peek()) {
                p.append(t.pop());
            }
            t.push(w);
        }
        if(t.size() > 0) {
            p.append(t.pop());
        }
        return p.toString();

    }
}
